package com.enjoyor.soa.traffic.server.tsms.handler;

import com.enjoyor.soa.traffic.api.dto.gps.PositionDto;
import com.enjoyor.soa.traffic.core.tsms.domain.MsgInfo;
import com.enjoyor.soa.traffic.server.tsms.common.RehearseGlobal;
import com.enjoyor.soa.traffic.util.handler.BasicMsgHandler;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 接收数据，数据清理
 */
public class ReceiveHandler extends BasicMsgHandler<PositionDto> {
    private ReceiveHandler() {
        super(1000, "接收实时数据");
    }

    private static ReceiveHandler single;

    public static ReceiveHandler getInstance() {
        if (single == null) {
            synchronized (ReceiveHandler.class) {
                if (single == null) {
                    single = new ReceiveHandler();
                    //最少5个线程 最多加到5个线程，积压20个累加
                    single.setMultiProcess(2, 20, 20);

                    // 推送实时数据，交由RealTimeHandler处理，过滤过期数据。//this.addNextHandler(RelayHandler.getInstance());
                    //single.addNextHandler(TrackHandler.getInstance());
                    // 交由RealTimeHandler处理，过滤过期数据。//this.addNextHandler(AlarmHandler.getInstance());
                }
            }
        }
        return single;
    }


    @Override
    protected boolean task(int taskTimes) {
        return true;
    }

    @Override
    protected boolean process(PositionDto msg) {

        // 过滤无坐标数据
        if (msg.getLocid() == null || msg.getLatitude() == null
                || msg.getLongitude() == null) {
            LoggerHelper.LOG.info("无locid或坐标数据，丢弃处理。locid,经纬度:"
                    + msg.getLocid() + "|" + msg.getLongitude() + "|"
                    + msg.getLatitude());
            return false;
        }
        // 避免null值
        if (msg.getAltitude() == null)
            msg.setAltitude(0.0);
        if (msg.getLocation() == null)
            msg.setLocation("");
        if (msg.getAlarm() == null)
            msg.setAlarm(0);

        // 经纬度搞反，（正常情况下，纬度范围-90~90，经度范围-180~180）
        double latitude = msg.getLatitude();
        double longitude = msg.getLongitude();
        if ((latitude < -90.0 || latitude > 90.0)
                && (longitude > -90.0 && longitude < 90.0)) {
            LoggerHelper.LOG.info("经纬度搞反，自动调换。locid,经纬度:" + msg.getLocid()
                    + "|" + msg.getLongitude() + "|" + msg.getLatitude());
            msg.setLatitude(longitude);
            msg.setLongitude(latitude);
        }

        // 为地位数据设置ID
        //msg.setId(CommonUtil.creatID());
        // 定位时间准确到秒，清除毫秒数据；  不接入超过数据保留时限的数据
        long time = msg.getUpdateTime().getTime();
        msg.setUpdateTime(new Date(time / 1000 * 1000));
        for (String key : RehearseGlobal.ActionDataPool.keySet()) {
            if (RehearseGlobal.ActionDataPool.get(key).get("positionDeviceId") != null)
                if (RehearseGlobal.ActionDataPool.get(key).get("positionDeviceId").toString().equals(msg.getLocid())) {
                	double speed = (double) Math.round(msg.getSpeed() * 100) / 100;
                    MsgInfo msginfo = new MsgInfo();
                    msginfo.setLongitude(msg.getLongitude());
                    msginfo.setLatitude(msg.getLatitude());
                    msginfo.setDirection(msg.getDirection());
                    msginfo.setExecuteTaskId(key);
                    msginfo.setLocid(msg.getLocid());
                    msginfo.setPlate("");
                    msginfo.setSpeed(speed);
                    msginfo.setUpdateTime(msg.getUpdateTime());
                    msginfo.setReceiveTime(new Date());
                    RehearseGlobal.ActionDataPool.get(key).put("x", msg.getLongitude());
                    RehearseGlobal.ActionDataPool.get(key).put("y", msg.getLatitude());
                    RehearseGlobal.ActionDataPool.get(key).put("speed", speed);
                    if(RehearseGlobal.ActionDataPool.get(key).get("position") != null 
                    		&& (boolean)RehearseGlobal.ActionDataPool.get(key).get("position")){
                    	com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
                    	Map<String, Object> routemap = new HashMap();
                        routemap.put("type", "point");
                        routemap.put("isDeviateCoord", true);//坐标偏离是否产生报警
                        routemap.put("mapLength", 0);//已行驶距离
                        routemap.put("speed", speed);//行驶速度
                        routemap.put("realX", msg.getLongitude());//实际定位坐标
                        routemap.put("realY", msg.getLatitude());
                        routemap.put("x", 0);//映射点坐标
                        routemap.put("y", 0);
                        routemap.put("percent", 0.0);
                        jsonObject.put(RehearseGlobal.ActionDataPool.get(key).get("routeId").toString(), routemap);
                        InitializeHandler.getInstance().getServerPushServiceImpl().pushMessage("" + key, jsonObject);
                    }else {
                    	MapingHandler.getInstance().handle(msginfo);
                    	
                    }
                    
                }
        }
        return false;
    }
}
